<?php

class MySqlConnect
{
    public function GetConn()
    {
    	//内网
//      $con = mysqli_connect("rdstbo42960uh0vfibx5.mysql.rds.aliyuncs.com", "jkroot", "Jkera2016", "lieren");
		//外网
        $con = mysqli_connect("jkera2016.mysql.rds.aliyuncs.com", "jkroot", "Jkera2016", "raisin_lucky");
//      $con = mysqli_connect("127.0.0.1", "root", "Jkera2016", "test");
        $con->query("set names utf8");
        return $con;
    }

    public function ExecuteData($sql)
    {
        $con = $this->GetConn();
        if ($con->query($sql)) {
        		mysqli_close($con);
            return 1;
        }else{
       	 	mysqli_close($con);
            return 0;
        }
    }
	
	public function InsertId($sql)
	{
		$con = $this->GetConn();
		$result = mysqli_query($sql,$con);
		return mysqli_insert_id();
		mysqli_close($con);
	}


    
	public function GetDataSetJson($sql)
    {
        $con = $this->GetConn();
        $result = $con->query($sql);
        if ($result) {
            $users = array();
            $i = 0;
            $fields = $result->fetch_fields();
            $length = count($fields);
            $count = $result->num_rows;
            $json = "";
            $json .= "{\"data1\":[";
            while ($row = $result->fetch_assoc()) {
                $json .= "{";
                for ($i = 0; $i <= $length - 1; $i++) {
                    $ziduan = $fields[$i]->name;
                    $json .= "\"" . $ziduan . "\":\"" . $row[$ziduan] . "\"";
                    if ($i < $length - 1) {
                        $json .= ",";
                    }
                }
                $json .= "}";
                if ($count > 1) {
                    $count--;
                    $json .= ",";
                }
            }
            $json .= "]}";
			mysqli_close($con);
            return $json;
            
        }
    }
	
	public function GetDataSetJsonPHP($sql)
    {
        $con = $this->GetConn();
        $result = $con->query($sql);
        if ($result) {
            $users = array();
            $i = 0;
            $fields = $result->fetch_fields();
            $length = count($fields);
            $count = $result->num_rows;
            $json = "";
            $json .= "[";
            while ($row = $result->fetch_assoc()) {
                $json .= "{";
                for ($i = 0; $i <= $length - 1; $i++) {
                    $ziduan = $fields[$i]->name;
                    $json .= "\"" . $ziduan . "\":\"" . $row[$ziduan] . "\"";
                    if ($i < $length - 1) {
                        $json .= ",";
                    }
                }
                $json .= "}";
                if ($count > 1) {
                    $count--;
                    $json .= ",";
                }
            }
            $json .= "]";
			mysqli_close($con);
            return $json;
            
        }
    }


}
?>